<!--  -->
<template>
  <div class="cartbottombar">
      <div class="checkImg" :class="{isCheckAll:isSelectAll}" @click="selectAll"><img src="~assets/img/cart/tick.svg" alt=""></div>
      <div @click="selectAll">全选</div>
      <div class="total">合计:<span>￥{{totalPrice}}</span></div>
      <div class="pay" @click="payClick">结算({{checkedLength}})</div>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  },
  methods:{
      selectAll() {
          if(this.isSelectAll) {
              for(let item of this.$store.state.car_info) {
                  item.checked = false
              }
          } else {
              this.$store.state.car_info.forEach(i => i.checked = true)
          }
      },
      payClick() {
          this.$emit('payClick')
      }
  },
  computed:{
      totalPrice() {
          var total = 0;
          for(let item of this.$store.state.car_info) {
              if(item.checked) {
                  total += item.price*item.count;
              }
          }
          return total.toFixed(2)
      },
      checkedLength() {
          return this.$store.state.car_info.filter(item => item.checked).length
      },
      isSelectAll() {
          if(this.$store.state.car_info.length === 0) return false;
        //find函数
            return !(this.$store.state.car_info.find(item => !item.checked))
        //普通遍历
        //   for(let item of this.$store.state.car_info){
        //       if(!item.checked) {
        //           return false
        //       }
        //   }
        //   return true
      }

  }
}
</script>

<style  scoped>
    .cartbottombar {
        display: flex;
        position: relative;
        width: 100%;
        height: 49px;
        line-height: 49px;
        position: absolute;
        bottom: 49px;
        right: 0;
        background-color: #fff;
        font-size: 14px;
        color:#999 ;
    }
    .checkImg {
        margin: 14px 5px 0 12px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        border: 1px solid #ccc;
        
    }
    .checkImg img {
        width: 100%;
        vertical-align: top;
    }
    .isCheckAll {
        background-color:tomato;
        border: none;
    }
    .total {
        flex: 1;
        color: #333;
        margin-right: 10px;
        text-align: right;
    }
    .total span {
        font-size: 18px;
        font-weight: 700;
        color: tomato;
    }
    .pay {
        width: 85px;
        height: 30px;
        margin-right: 10px;
        margin-top: 9px;
        line-height: 30px;
        text-align: center;
        background-color: tomato;
        color: #fff;
        font-size: 16px;
        font-weight: 700;
        border-radius: 30px;
        
    }
</style>
